<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:ram="urn:un:unece:uncefact:data:draft:ReusableAggregateBusinessInformationEntity:1"
	xmlns:rsm="urn:un:unece:uncefact:data:draft:Test:1">
	
	<xsl:output encoding="UTF-8"  indent="yes" />
	
	<xsl:template match="/">
		<FoodserviceInvoice 
			xmlns="urn:un:unece:uncefact:data:draft:Test:1"  
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
			xsi:schemaLocation="urn:un:unece:uncefact:data:draft:Test:1 FoodserviceInvoice.xsd">
			<Identifier><xsl:call-template name="GetInvoiceIdentifier" /></Identifier>
			<OrderReference>
				<xsl:value-of select="/rsm:FoodservicePurchaseOrder/rsm:Identifier" />
			</OrderReference>
			<DespatchAdviceReference>
				<xsl:call-template name="GetDespatchAdviceIdentifier" />
			</DespatchAdviceReference>
			<IssueDate>
				<xsl:value-of select="/rsm:FoodservicePurchaseOrder/rsm:IssueDate" />
			</IssueDate>
			<DeliveryDate>
				<xsl:value-of select="/rsm:FoodservicePurchaseOrder/rsm:DeliveryDate" />
			</DeliveryDate>
			<Currency>EURO</Currency>
			<xsl:sequence select="/rsm:FoodservicePurchaseOrder/rsm:Buyer" />
			<xsl:sequence select="/rsm:FoodservicePurchaseOrder/rsm:Seller" />
			<Invoicee>
				<xsl:sequence select="/rsm:FoodservicePurchaseOrder/rsm:Buyer/ram:ID" />
			</Invoicee>
			<xsl:sequence select="/rsm:FoodservicePurchaseOrder/rsm:ShipTo" />
			<xsl:for-each select="/rsm:FoodservicePurchaseOrder/rsm:Item">
				<xsl:if test="ram:OrderItemID != '' ">
				<xsl:variable name="prod.id" select="ram:ProductID"/>
				<InvoiceItem>
					<ram:InvoiceItemID>
						<xsl:value-of select="ram:OrderItemID" />
					</ram:InvoiceItemID>
					<ram:ProductID>
						<xsl:value-of select="$prod.id" />
					</ram:ProductID>
					<ram:Amount>
						<xsl:value-of 
							select="document( 'testdata.xml' )/TestData
							/Product[@id = $prod.id]/Amount * ram:Quantity" />
					</ram:Amount>
					<ram:DeliveredQuantity>
						<xsl:value-of select="ram:Quantity" />
					</ram:DeliveredQuantity>
					<ram:InvoicedQuantity>
						<xsl:value-of select="ram:Quantity" />
					</ram:InvoicedQuantity>
					<ram:DepositAllowancesCharges>
						<ram:Amount>
							<xsl:value-of 
								select="document( 'testdata.xml' )/TestData
									/Product[@id = $prod.id]/DepositAllowancesCharges/Amount" />
						</ram:Amount>
						<ram:Tax>
							<ram:BaseAmount>
								<xsl:value-of 
									select="document( 'testdata.xml' )/TestData
									/Product[@id = $prod.id]/DepositAllowancesCharges/Amount" />
							</ram:BaseAmount>
							<ram:Type>BTW</ram:Type>
							<ram:Rate>19</ram:Rate>
						</ram:Tax>
					</ram:DepositAllowancesCharges>
				</InvoiceItem>
				</xsl:if>
			</xsl:for-each>
			<HandlingAllowancesCharges>
				<ram:Amount>10</ram:Amount>
				<ram:Tax>
					<ram:BaseAmount>10</ram:BaseAmount>
					<ram:Type>BTW</ram:Type>
					<ram:Rate>19</ram:Rate>
				</ram:Tax>
			</HandlingAllowancesCharges>
			<LinesTotalAmount>
				<xsl:call-template name="GetLinesTotalAmount" />
			</LinesTotalAmount>
			<VATTotalAmount>
				<xsl:call-template name="GetVATTotalAmount" />
			</VATTotalAmount>
			<InvoiceTotalAmount>
				<xsl:variable name="lines.total.amount">
					<xsl:call-template name="GetLinesTotalAmount" />
				</xsl:variable>
				<xsl:variable name="vat.total.amount">
					<xsl:call-template name="GetVATTotalAmount" />
				</xsl:variable>
				<xsl:value-of select="$lines.total.amount + $vat.total.amount + 11.90"/>				
			</InvoiceTotalAmount>
		</FoodserviceInvoice>
	</xsl:template>
	
	<xsl:template name="GetInvoiceIdentifier">
		<xsl:variable name="po.id" select="/rsm:FoodservicePurchaseOrder/rsm:Identifier" />
		
		<xsl:sequence select="concat( 'FSI-', $po.id )" />
	</xsl:template>
	
	<xsl:template name="GetDespatchAdviceIdentifier">
		<xsl:variable name="po.id" select="/rsm:FoodservicePurchaseOrder/rsm:Identifier" />
		
		<xsl:sequence select="concat( 'FSDA-', $po.id )" />
	</xsl:template>
	
	<xsl:template name="GetLinesTotalAmount">
		<xsl:variable name="lines.total.amounts">
			<xsl:call-template name="GetLinesTotalAmounts" />
		</xsl:variable>
		
		<xsl:value-of select="sum( $lines.total.amounts/amount )" />
	</xsl:template>
	
	<xsl:template name="GetLinesTotalAmounts" as="item()*">
		<xsl:for-each select="/rsm:FoodservicePurchaseOrder/rsm:Item">
			<xsl:if test="ram:OrderItemID != '' ">
			<xsl:variable name="prod.id" select="ram:ProductID"/>
			<amount>
				<xsl:value-of
					select="document( 'testdata.xml' )/TestData
					/Product[@id = $prod.id]/Amount * ram:Quantity" />
			</amount>
			</xsl:if>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template name="GetVATTotalAmount">
		<xsl:variable name="lines.total.amount">
			<xsl:call-template name="GetLinesTotalAmount" />
		</xsl:variable>
		
		<xsl:value-of select="$lines.total.amount * 0.19" />
	</xsl:template>
</xsl:stylesheet>
